package io.github.xxyopen.novel.controller.front;

import io.github.xxyopen.novel.core.auth.UserHolder;
import io.github.xxyopen.novel.core.common.resp.PageRespDto;
import io.github.xxyopen.novel.core.common.resp.RestResp;
import io.github.xxyopen.novel.dao.entity.Practice;
import io.github.xxyopen.novel.dao.entity.PracticeComment;
import io.github.xxyopen.novel.dao.entity.StuBehavior;
import io.github.xxyopen.novel.dao.entity.StuPractice;
import io.github.xxyopen.novel.dto.req.UserCommentReqDto;
import io.github.xxyopen.novel.dto.resp.UserCommentRespDto;
import io.github.xxyopen.novel.service.PracticeService;
import io.github.xxyopen.novel.service.RecommendService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/practice")
public class PracticeController {

    @Autowired
    private PracticeService practiceService;

    @Autowired
    private RecommendService recommendService;

    /**
     * 客户端轮播图
     * @return
     */
    @GetMapping("/list/slider")
    public RestResp<List<Practice>> getListSlider() {
        return RestResp.ok(practiceService.getListSlider());
    }

    //文章详情
    @GetMapping("/{id}")
    public RestResp<Practice> getPracticeById(@PathVariable Integer id) {
        return practiceService.getPracticeById(id);
    }

    // 文章点击
    @PostMapping("/click")
    public RestResp<Void> clickPractice(@RequestBody StuBehavior stuBehavior) {
        practiceService.clickPractice(stuBehavior);
        return RestResp.ok(null);
    }

    // 文章评论
    /**
     * 发表评论接口
     */
    @Operation(summary = "发表评论接口")
    @PostMapping("/comment")
    public RestResp comment(@RequestBody PracticeComment practiceComment) {
        return practiceService.addComment(practiceComment);
    }

    @GetMapping("/comments")
    public RestResp<PageRespDto<PracticeComment>> getPracticeComments(
            @RequestParam("practiceId") Long practiceId,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam("pageNum") Integer pageNum
    ) {
        return practiceService.getComments(practiceId, pageNum, pageSize);
    }

    @PostMapping("/signup")
    public RestResp signup(@RequestBody StuPractice stuPractice) {
        return practiceService.singUpPractice(stuPractice);
    }

    @GetMapping("/status")
    public RestResp getPracticeStatus(@RequestParam("stuId") Long stuId, @RequestParam("practiceId") Long practiceId){
        return practiceService.getPracticeStatus(stuId, practiceId);
    }

    @PutMapping("/update")
    public RestResp updatePractice(@RequestBody Practice practice) {
        practiceService.updatePractice(practice);
        return RestResp.ok(null);
    }

    // 添加或删除学生点赞、收藏行为
    @PostMapping("/behavior/likeorcollect")
    public RestResp likeOrCollectPractice(@RequestBody StuBehavior stuBehavior){
        practiceService.likeOrCollectPractice(stuBehavior);
        return RestResp.ok(null);
    }

    @GetMapping("/mysignuppractice")
    public RestResp getMySignUpPractice(
            @RequestParam("stuId") Long stuId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){
        return practiceService.getMySignUpPractice(stuId, pageNum, pageSize);
    }

    @GetMapping("/mycollection")
    public RestResp getMyCollection(
            @RequestParam("stuId") Long stuId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){
        return practiceService.getMyCollection(stuId, pageNum, pageSize);
    }

    // 最近发布
    @GetMapping("/recentpublish")
    public RestResp getRecentPublish(){
        return practiceService.getRecentPublish();
    }

    // 本周热门
    @GetMapping("/thisweekhot")
    public RestResp getThisWeekHot(){
        return practiceService.getThisWeekHot();
    }

    // 近期截止
    @GetMapping("/recentdeadline")
    public RestResp getRecentDeadline(){
        return practiceService.getRecentDeadline();
    }

    // 点击榜单
    @GetMapping("/clickrank")
    public RestResp getClickRank(){
        return practiceService.getClickRank();
    }

    // 搜索实践
    @GetMapping("/search")
    public RestResp searchPractice(
            Integer pageNum,
            Integer pageSize,
            String key,
            String sort
    ){

        return practiceService.searchPractice(pageNum, pageSize, key, sort);
    }

    // 推荐实践
    @GetMapping("/recommend")
    public RestResp getRecommendPractice(Long stuId){

        List<Practice> practices = recommendService.doubleRecommendation(stuId);
        return RestResp.ok(practices);
    }
}
